package day2;

import java.util.Arrays;
import java.util.Scanner;

public class Test2 {

	public static void main(String[] args) {
		int num=getNum();
		System.out.println("输入的数字是："+num);
		int nums[]=splitNum(num);
		System.out.println("拆分之后的数字："+Arrays.toString(nums));//将数组转换成字符串打印
		//求和运算
		int sum = sum(nums);
		System.out.println("个位数字之和："+sum);
		//判断是否是质数
		System.out.println(sum+(isPri(sum)?"是":"不是")+"质数");
		if(isPri(sum )){
			huiwen(nums,num);
		}else{
			int diffnums= diffNums(nums);
			diamond(diffnums);
		}
		
		
	}
	//空心菱形
	private static void diamond(int row){
		for(int i =1;i<=row;i++){
			for(int t=1;t<=row-i;t++){
				System.out.print(" ");
			}
			for(int j=1;j<=2*i-1;j++){
				if(j==1 || j==2*i-1){
					System.out.print(row);
				}else{
					System.out.print(" ");
				}

			}
			System.out.println();
		}
		for(int i =row-1;i>=1;i--){
			for(int t=1;t<=row-i;t++){
				System.out.print(" ");
			}
			for(int j=1;j<=2*i-1;j++){
				if(j==1 || j==2*i-1){
					System.out.print(row);
				}else{
					System.out.print(" ");
				}

			}
			System.out.println();
		}
	}
	
	//不同数字的个数
	private static int diffNums(int[] nums) {
		// TODO Auto-generated method stub
		int sum =0;
		for(int i=0;i<=9;i++){
			for(int j=0;j<nums.length;j++){
				if(i == nums[j]){
					sum++;
					break;
				}
			}
		}
		System.out.println("不同数字的个数"+sum);
		return sum;

	}

	private static void huiwen(int[] nums, int num) {
		// TODO Auto-generated method stub
		StringBuffer sb = new StringBuffer();
		for(int temp:nums){
			sb.append(temp);
		}
		if(Integer.parseInt(sb.toString()) == num){
			System.out.println(num+"是回文数");
		}else{
			System.out.println(num+"不是回文数");

		}
		
	}

	//判断是否是质数
	public static boolean isPri(int num){
		for(int i=2 ;i <= num/2; i++){
			if(num % i==0){
				return false;
				
			}
		}
		return true;
		
	}
	
	
	private static int sum(int[] nums) {
		// TODO Auto-generated method stub
		int sum=0;
		for(int temp : nums){
			sum = sum +temp;
		}
		return sum;
	}

	//将数字拆分，存到数组中
	public static int [] splitNum(int num){
		int [] nums = new int [(num+"").length()];
		int index=0;
		while(num !=0){
			int temp = num;
			temp = num %10;
			num =num/10;
			nums[index]=temp;
			index ++;
		}
		return nums;
		
	}
	
	//录入2-9位数字
	private static int getNum() {
		// TODO Auto-generated method stub
		do{
			System.out.println("请输入2-9位的正整数：");
			Scanner input= new Scanner(System.in); 
			
			//判断是否2-9位数字
			try{
				int num=input.nextInt();
				if(num>=10 && num<=999999999){
					return num;
				}else{
					System.out.println("输入不合规则，请重新输入：");
				}
			}catch(Exception e){
				System.out.println("输入不合规则，请重新输入：");
			}
			
		}while(true);
		
	}
	
    
}
